package ldkxingzhe.l365calendar.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;

/**
 * Preference辅助类
 * Created by ke on 16-1-14.
 */
public class PreferenceHelper {

    private final static String DEFAUL_PREFERENCE_NAME = "Default";

    private final SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;

    /**
     * @see PreferenceHelper(Context, String)
     */
    public PreferenceHelper(Context context){
        this(context, DEFAUL_PREFERENCE_NAME);
    }

    /**
     * 构造函数
     * 以MODE_PRIVATE 方式打开fileName的文件
     * @param context     上下文
     * @param fileName    文件名
     */
    public PreferenceHelper(Context context, @NonNull String fileName){
        sharedPreferences = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
    }

    public String getString(String key){
        return sharedPreferences.getString(key, null);
    }

    public int getInt(String key){
        return sharedPreferences.getInt(key, 0);
    }

    public boolean getBoolean(String key){
        return sharedPreferences.getBoolean(key, false);
    }

    public long getLong(String key){return sharedPreferences.getLong(key, 0);}

    public SharedPreferences.Editor getEditor(){
        if(editor == null){
            editor = sharedPreferences.edit();
        }
        return editor;
    }
}
